<template>
  <div>
    <div class="main">
      <eagle-input
        label="风险点名称"
        name="name"
        :value="data.RiskPointName"
        required
        placeholder="请输入"
        @input="ChangeVal1"
        isInputText
      ></eagle-input>
      <eagle-input
        readonly
        label="风险点类型"
        v-model="data.RPTypeName"
        name="rPTypeName"
        islink
        required
        @click="_chooseRPType"
      ></eagle-input>
      <eagle-input
        readonly
        name="OrgName"
        v-model="data.OrgName"
        label="部门/车间"
        placeholder="请选择"
        required
        @click-input="goDeptment"
      >
        <template #button>
          <van-icon name="cross" @click="clearOrgCode" />
        </template>
      </eagle-input>
      <eagle-input
        readonly
        name="calendar"
        v-model="data.TeamName"
        label="所属班组"
        placeholder="请选择"
        @click-input="goTeamListItem"
      >
        <template #button>
          <van-icon name="cross" @click="clearTeam" />
        </template>
      </eagle-input>
      <eagle-input
        readonly
        name="calendar"
        v-model="data.OrgPostName"
        label="所属岗位"
        placeholder="请选择"
        @click-input="goPostListItem"
      >
        <template #button>
          <van-icon name="cross" @click="clearPost" />
        </template>
      </eagle-input>
      <eagle-input
        label="设备工具"
        placeholder="请输入"
        name="equipmentName"
        :value="data.EquipmentName"
        @input="ChangeVal2"
        isInputText
      ></eagle-input>
      <eagle-input
        label="危险介质"
        placeholder="请输入"
        name="chemName"
        :value="data.ChemName"
        @input="ChangeVal3"
        isInputText
      ></eagle-input>
      <eagle-input
        label="职业病危害因素"
        placeholder="请输入"
        name="hazardName"
        :value="data.HazardName"
        @input="ChangeVal4"
        isInputText
      ></eagle-input>

      <div class="btn_submit">
        <eagle-button block type="primary" @click="_submit"> 保存 </eagle-button>
      </div>

      <deptment-choose
        :show.sync="deptmentShow"
        :selectedValue="data.OrgCode"
        @callback="_chooseDeptmentCb"
        :isMulti="false"
        ref="deptmentChoose"
    >
    </deptment-choose>
    <TeamChoose
      :show.sync="TeamShow"
      :orgCode="data.OrgCode"
      :selectedValue="data.TeamCode"
      @callback="_chooseTeamCb"
    ></TeamChoose>
      <PostChoose
        title="选择岗位"
        :show.sync="PostShow"
        :selectedValue="data.OrgPostCode"
        @callback="_choosePostCb"
      ></PostChoose>
    </div>
    <eagle-dialog
      v-model="showRPType"
      v-if="showRPType"
      title="选择风险点类型"
	    :titlePosition="'center'"
      :showButtons="false"
      :closeOnClickOverlay="true"
    >
      <eagle-form>
        <div style="margin:0 25px">
          <eagle-radio-group name="radio" label="" v-model="data.RPType" @change="radioChange" :dataSource="data.RPTypeList" direction="horizontal"></eagle-radio-group>
        </div>
        <div style="margin: 16px"></div>
      </eagle-form>
    </eagle-dialog>
  </div>
</template>

<script>
import UserChoose from "@/views/components/user/userChooseDialog.vue";
import TeamChoose from "@/views/components/user/userTeamChooseDialog.vue";
import PostChoose from "@/views/components/user/userPostChooseDialog.vue";
import DeptmentChoose from "@/views/components/department/departmentChooseDialog.vue";

export default {
  components: {
    UserChoose,
    DeptmentChoose,
    TeamChoose,
    PostChoose,
  },
  data() {
    return {
      menuList: [],
      mobile: "",
      message: "",
      value: "",
      radio: "1",
      checkbox: false,
      checkboxGroup: [],
      UserChooseShow: false,
      showRPType: false,
      deptmentShow: false,
      TeamShow: false,
      PostShow: false,
      userChooseType: "",
      userChooseName: "",
      data: {},
      id: 0,
    };
  },

  created() {
    this.initData();
  },
  methods: {
    ChangeVal1(val) {
      this.data.RiskPointName = val;
    },
    ChangeVal2(val) {
      this.data.EquipmentName = val;
    },
    ChangeVal3(val) {
      this.data.ChemName = val;
    },
    ChangeVal4(val) {
      this.data.HazardName = val;
    },
    // 部门
    goDeptment() {
      this.deptmentShow = true;
    },
    _chooseDeptmentCb(item) {
      this.data.OrgName = item.name;
      this.data.OrgCode = item.code;
      this.deptmentShow = false;
    },
    clearOrgCode() {
      this.data.OrgCode = null;
      this.data.OrgName = null;
    },
    // 班组
    goTeamListItem() {
      this.TeamShow = true;
    },
    _chooseTeamCb(item) {
      this.data.TeamName = item.name || ""
      this.data.TeamCode = item.code || "";
    },
    clearTeam() {
      this.data.TeamName = null;
      this.data.TeamCode = null;
    },
    // 岗位
    goPostListItem() {
      this.PostShow = true;
    },
    _choosePostCb(item) {
      this.data.OrgPostCode = item.code || "";
      this.data.OrgPostName = item.name || "";
    },
    clearPost() {
      this.data.OrgPostName = null;
      this.data.OrgPostCode = null;
    },
    initData() {
      let _this = this;
      if (_this.$route.query.id) {
        _this.id = _this.$route.query.id;
      }
      if (_this.id == 0) {
        _this.$axios
        .get("/RiskPoint/InitNewEntity",{params:{id: _this.id}})
        .then((res)=>{
          _this.data = res.data;
        })
        .catch((err) => {
          console.log("err", err);
        });
        _this.$axios.get("/RiskPoint/GetRiskPointTypeParams",{}).then((res)=>{
          _this.data.RPTypeList = res.data;
        })
      }
    },
    // 风险点类型
    _chooseRPType() {
      this.showRPType = true;
    },
    radioChange(names) {
      let select = this.data.RPTypeList.filter((item) => item.ID == names);
      this.data.RPTypeName = select[0].Name;
      this.data.RPType = select[0].ID;

      this.showRPType = false;
    },
    // 保存
    _submit() {
      if (!this.data.RiskPointName) {
        this.$toast("请输入风险点名称");
        return;
      }
      if (!this.data.OrgName) {
        this.$toast("请选择部门/车间");
        return;
      }
      if (!this.data.RPTypeName) {
        this.$toast("请选择风险点类型");
        return;
      }
      let _this = this;
      _this.$axios.post("/RiskPoint/PostData",_this.data)
      .then((res)=>{
        if (res.success) {
          _this.$toast.success("保存成功");
          setTimeout(() => {
            _this.$router.goRefresh();
          }, 500);
        }
      })
      .catch((err) => {
        console.log("err", err);
      });
    },
  },
};
</script>

<style lang="less">
.main {
  padding-top: 10px;
}
.input_text .van-field__control {
  color: #999 !important;
}
.popup-title {
  line-height: 35px;
  padding: 10px;
  font-size: 15px;
  padding-left: 20px;
}
</style>
